<div class="tutorial-bundle" data-metadata-json='{"programKind": "per-method", "difficulty": "getting started"}' data-tip-sha1="711faf60356f16c8c8a04e5d66313b5419fc42b0"><div class="front-matter" data-complete-code-text='import pytch
import random


class Stage(pytch.Stage):
    Backdrops = ["solid-white.png"]


class Snake(pytch.Sprite):
    Costumes = ["Pytch-hello-snake.png"]

    @pytch.when_this_sprite_clicked
    def speak(self):
        self.say_for_seconds("Hello there!", 2.0)
' data-initial-code-text='import pytch
import random


class Stage(pytch.Stage):
    Backdrops = ["solid-white.png"]
'><h1>Hello there!</h1><p>In this tutorial, you will learn how to create and run a Pytch project
with a <em>sprite</em>.</p></div><div class="chapter-content"><h2>What is a Pytch program?</h2><p>Pytch programs are written in a programming language called <em>Python</em>.
As well as the normal Python features, Pytch also has extra commands
for sprites, sounds, and other things.  You make a Pytch project by
writing a Python program in the Pytch webapp.</p><div class="learner-task"><div class="learner-task-intro">
<p>Although you haven't written any code yet, you can still run the
project, by clicking the green play button.  What happens?</p>
</div><div class="learner-task-help">
<p>Nothing!  You haven't made any sprites or written any scripts yet.</p>
</div></div></div><div class="chapter-content"><h2>Create the sprite</h2><p>To start, you will make a sprite that the player will control.</p><div class="learner-task"><div class="learner-task-intro">
<p>Create a new sprite called <strong>Snake</strong>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-sprite", "name": "Snake"}' data-slug="create-snake-class"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your project, by clicking the green play button.  What happens?</p>
</div><div class="learner-task-help">
<p>Nothing!  You haven't given the sprite any <em>costumes</em> yet.</p>
</div></div><p>The sprite has no <em>costumes</em> to say what it looks like.  The next
chapter will fix this.</p></div><div class="chapter-content"><h2>Add a costume</h2><p>To say what a sprite looks like, you give it <em>costumes</em>.  For this
tutorial, the sprite will only have one costume, but in general, a
sprite can have lots of costumes, and your program can switch between
them.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a suitable costume to your sprite.  There is one called
<strong>Pytch-hello-snake</strong> in the media library.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-medialib-appearances-entry", "actor": {"kind": "sprite", "name": "Snake"}, "displayIdentifier": "Pytch-hello-snake.png", "nItems": 1}' data-slug="snake-costume"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your project, by clicking the green play button.  You should see a
snake in the middle of the stage.</p>
</div></div></div><div class="chapter-content"><h2>Add a script</h2><p>You will now make the Snake react when you click on it.  A sprite can
have <em>scripts</em> — these are pieces of Python code which run when
certain things happen, for example, when the sprite is clicked, or
when a particular key is pressed.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a script which will run when the sprite is clicked.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Snake"}, "methodName": "speak"}, "event": {"kind": "clicked"}, "codeText": ""}' data-slug="add-when-clicked-script"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your project, by clicking the green play button.  What happens
when you click on the Snake?</p>
</div><div class="learner-task-help">
<p>Nothing!  You have not written any Python code yet.</p>
</div></div><p>Nothing happens when you click on the Snake, because the script is
<em>empty</em> — it has no Python code in it.  The next chapter will fix
this.</p><h3>Write the code for the script</h3><p>Now you can make the Snake say something when it's clicked.</p><div class="learner-task"><div class="learner-task-intro">
<p>Write Python code to make the Snake say <em>Hello there!</em> when you click
on it.</p>
</div><div class="learner-task-help">
<p>You have already made a script, so you just need to write a line of
Python code into that script.</p>
</div><div class="learner-task-help">
<p>If you know Scratch, you might have used code like</p>
<pre><code class="language-scratch">say [Hello there!] for [2.0] seconds
</code></pre>
<p>Look in the help to learn how to do this in Python.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Snake"}, "methodName": "speak"}, "event": {"kind": "clicked"}, "oldCodeText": "", "newCodeText": "self.say_for_seconds(\"Hello there!\", 2.0)"}' data-slug="add-script-body"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your project, by clicking the green play button.  The Snake should
say your message when you click on it.</p>
</div></div></div><div class="chapter-content"><h2>The project is finished!</h2><p>Congratulations on your first Pytch project!</p><h3>Challenges</h3><p>Can you change your program to complete these challenges?</p><ul>
<li>
<p>Make the Snake say <code>"Hi there!"</code> instead of <code>"Hello there!"</code>.</p>
</li>
<li>
<p>Make the Snake's speech balloon appear for 3 seconds instead of
  2 seconds.</p>
</li>
<li>
<p>(More advanced:) Make the Snake say two things, one after the other.
  Maybe it could say <code>"Hi there!"</code> for two seconds, and then <code>"OK,
  bye!"</code> for two seconds.</p>
</li>
</ul><h3>Credits</h3><div class="asset-credits"><p class="credit-intro">For <code class="asset-filename">solid-white.png</code> (used in the project):</p><div class="credits"><p>Needed for demo.</p></div><p class="credit-intro">For <code class="asset-filename">Pytch-hello-snake.png</code> (used in the project):</p><div class="credits"><p>The Pytch snake image is Copyright 2024 Trinity College Dublin.  You
are welcome to use it under the terms of the Creative Commons
Attribution-ShareAlike 4.0 International license.</p></div><p class="credit-intro">For <code class="asset-filename">screenshot.png</code> (used in the tutorial text/summary):</p><div class="credits"><p>The ‘screenshot’ image is Copyright 2024 Trinity College Dublin.  You
are welcome to use it under the terms of the Creative Commons
Attribution-ShareAlike 4.0 International license.</p></div></div></div></div>